package com.dqlp.yf.rabbitmq.receive;

import com.dqlp.yf.rabbitmq.constant.MqConstant;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @program: rabbitmq
 * @description: 死信队列消费者
 * @author: andy
 * @create: 2019-09-07 16:25
 */
@Component
@Slf4j
public class DlxCoustomer {
    @RabbitListener(queues = MqConstant.DIRECT_QUEUE)
    public void customer(Message message, Channel channel) throws IOException, InterruptedException {
        channel.basicQos(0, 1, false);
       // String correlationId = message.getMessageProperties().getCorrelationId(); //根本拿到
        log.info("*******************");
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        log.info("-----------consume message----------");
        log.info("body: " + new String(message.getBody()));
        log.info("deliveryTag为：" + deliveryTag);
        if(deliveryTag%2==0){
            channel.basicAck(deliveryTag, false);
        }else {
            channel.basicNack(deliveryTag, false, false);
        }


    }

    @RabbitListener(queues = MqConstant.DLX_QUEUE)
    public void dlxCustomer(Message message, Channel channel) throws IOException, InterruptedException {
        channel.basicQos(0, 1, false);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.err.println("死信consume message----------");
        System.err.println("死信 body: " + new String(message.getBody()));
        Integer num = (Integer) message.getMessageProperties().getHeaders().get("num");
        log.info("死信队列里面的deliveryTag为：" + deliveryTag);
        log.info("私信队列准备ack消息了");
        channel.basicAck(deliveryTag, false);

    }
}
